package org.learn.intermediate;

import java.util.*;

public class MapFor {
    public static void main(String[] args) {
        HashMap hashMap = new HashMap();
        hashMap.put("邓超","孙俪");
        hashMap.put("鹿晗","关晓彤");
        hashMap.put("宋捷","马蓉");
        hashMap.put("邓超","null");
        hashMap.put("null","刘亦菲");
        hashMap.put("胡歌","彭于晏");
        //
        System.out.println("----------第一种方式----------");
        Set keySetset = hashMap.keySet();
        //
        for (Object key : keySetset) {
            System.out.println(key + "-" + hashMap.get(key));
        }
        //迭代器
        System.out.println("----------第二种方式----------");
        Iterator iterator = keySetset.iterator();
        while (iterator.hasNext()) {
            Object key =  iterator.next();
            System.out.println(key + "-" + hashMap.get(key));
        }
        //取出所有value
        Collection values = hashMap.values();
        System.out.println("-----------取出所有value---------");
        //增强for循环
        for (Object value : values) {
            System.out.println(value);
        }
        //迭代器
        System.out.println("-----------取出所有value 迭代器---------");
        Iterator iterator1 = values.iterator();
        while (iterator1.hasNext()) {
            Object value =  iterator1.next();
            System.out.println(value);
        }
        //通过EntrySet 来获取k-v
        System.out.println("-----------使用 EntrySet---------");
        Set entrySet = hashMap.entrySet();
        //增强for循环
        for (Object entry : entrySet) {
            //将entry转成Map。entry
            Map.Entry m = (Map.Entry) entry;
            System.out.println(m.getKey() + " -" + m.getValue());
        }
        //迭代器
        System.out.println("-----------使用 EntrySet迭代器---------");
        Iterator iterator2 = entrySet.iterator();
        while (iterator2.hasNext()) {
            Object entry =  iterator2.next();
            Map.Entry m = (Map.Entry) entry;
            System.out.println(m.getKey() + " -" + m.getValue());
        }


    }
}
